package com.xbj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xbj.domain.*;
import com.xbj.mapper.EmpMapper;
import com.xbj.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("empService")
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    public List<Map<String, Object>> findUser() {
        return empMapper.findUser();
    }

    public List<Map<String, Object>> findDxUser() {
        return empMapper.findDxUser();
    }

    @Override
    public Map<String, Object> getEmpDesc(Integer id) {
        Map<String, Object> map = empMapper.getEmpDesc(id);
        return map;
    }

    @Override
    public Map<String, Object> getEmpDescByZx(Integer id) {
        Map<String, Object> map = empMapper.getEmpDescByZx(id);
        return map;
    }

    @Override
    public int addEmp(Emp emp) {
        if (emp == null) {
            return 0;
        }
        int empId = empMapper.addEmp(emp);
        if (empId == 0) {
            return 0;
        }
        // 设置电销对象中的对应学员ID
        emp.getEmpdxdesc().setEmpId(emp.getId());
        // 创建咨询对象信息,保存电销中信息
        int i = empMapper.saveEmpDxDesc(emp.getEmpdxdesc());
        return i;
    }

    @Override
    public PageInfo<Emp> getListByDX(int currentNum, int pageSize, Emp emp) {

        PageHelper.startPage(currentNum, pageSize);

        List<Emp> list = empMapper.findEmpListByDX(emp);

        return new PageInfo<Emp>(list);
    }

    @Override
    public int saveDxDesc(EmpDxDesc empdxdesc) {
        return empMapper.saveDxDesc(empdxdesc);
    }

    @Override
    public int saveDxToZxRecord(Integer empId, String zxDate, String zxId) {
        Map<String, Object> map = new HashMap<>();
        map.put("empId", empId);
        map.put("zxDate", zxDate);
        map.put("zxId", zxId);
        int i = empMapper.saveDxToZxRecord(map);
        if (i == 1) {
            Map<String, Object> maps = new HashMap<>();
            map.put("empId", empId);
            map.put("dxstatus", 1);
            empMapper.updateDxStatus(maps);
        }
        return i;
    }

    @Override
    public EmpZxDesc findZxRecord(Integer empid) {
        return empMapper.findZxRecord(empid);
    }

    @Override
    public int updateDxToZxRecord(Integer zxdescid, Integer empId, String zxDate, String zxId) {
        Map<String, Object> map = new HashMap<>();
        map.put("zxdescid", zxdescid);
        map.put("empId", empId);
        map.put("zxDate", zxDate);
        map.put("zxId", zxId);
        int i = empMapper.updateDxToZxRecord(map);
        if (i == 1) {
            Map<String, Object> maps = new HashMap<>();
            map.put("empId", empId);
            map.put("dxstatus", 1);
            empMapper.updateDxStatus(maps);
        }
        return i;
    }

    public int updateDxToDxRecord(Integer empId, Integer dxId, Date dxDate) {
        return empMapper.updateDxToDxRecord(empId, dxId, dxDate);
    }

    @Override
    public PageInfo<Emp> getListByZX(int currentNum, int pageSize, Emp emp) {
        PageHelper.startPage(currentNum, pageSize);

        List<Emp> list = empMapper.findEmpListByZX(emp);

        return new PageInfo<Emp>(list);
    }

    @Override
    public int saveZxDesc(EmpZxDesc empzxdesc) {
        return empMapper.saveZxDesc(empzxdesc);
    }

    @Override
    public int updateEmp(Integer empId, String field, String newValue) {
        Map<String, Object> map = new HashMap<>();
        map.put("empId", empId);
        map.put("field", field);
        map.put("fieldValue", newValue);
        return empMapper.updateEmp(map);
    }

    @Override
    public boolean findEmpPermissionById(Integer userId) {
        Role r = empMapper.findEmpByLoginId(userId);
        if (r.getId() == 1) {
            return true;
        }
        return false;
    }

    @Override
    public void updateEmpStatus(Integer empId) {
        empMapper.updateEmpStatus(empId);
    }

    @Override
    public void updateEmpState(Integer empId) {
        empMapper.updateEmpState(empId);
    }

    @Override
    public List<Emp> findEmpExcel(List<Integer> ids) {
        List<Emp> list = empMapper.findEmpExcel(ids);
        return list;
    }

    @Override
    public int saveEmpDetail(EmpDetail empDetail) {
        return empMapper.saveEmpDetail(empDetail);
    }

    @Override
    public int updateEmpDetail(EmpDetail empDetail) {
        // TODO Auto-generated method stub
        return empMapper.updateEmpDetail(empDetail);
    }

    @Override
    public EmpDetail findEmpDetailByEmpId(Integer empId) {
        return empMapper.findEmpDetailByEmpId(empId);
    }

    @Override
    public PageInfo<Emp> findEmpByDetailApplyDate(int currentNum, int pageSize, Map<String, Object> map) {
        PageHelper.startPage(currentNum, pageSize);
        List<Emp> list = empMapper.findEmpByDetailApplyDate(map);

        return new PageInfo<Emp>(list);
    }


}
